// 这个JS文件就是专门用于管理请求各种接口地址的
import { Promise } from "core-js";
import Network from "./network";
// 调用getBanner方法它底层就会去发送一个get请求到这个地址
// 轮播图
export const getBanner = () => Network.get('/banner?type=2'); // 这里的地址自动拼接network中的axios.defaults.baseURL地址
// 推荐歌单
// export const getPersonalized = () => Network.get('/personalized?limit=6');
export const getPersonalized = () => Network.get('/personalized');
// 最新专辑
export const getNewAlbum = () => Network.get('/album/newest');
// 最新歌曲
export const getNewsong = () => Network.get('/personalized/newsong');
// 获取歌单详情
export const getPlayList = (id) => Network.get(`/playlist/detail?id=${id}`);
// 获取专辑内容
export const getAlbum = (data) => Network.get('/album', data);
// 获取歌曲详情
export const getSongDetail = (data) => Network.get('/song/detail', data);
// 获取歌词
export const getLyric = (data) => Network.get('/lyric', data);
// 获取播放歌曲URL
export const getSongURL = (data) => Network.get('/song/url', data);
//获取歌手单曲
export const getArtistsSongs = (data) => Network.get('/artists', data);
// 获取热门歌手
// export const getHotArtists = () => Network.get('/top/artists?offset=0&limit=5');
export const getHotArtists = () => {
    return new Promise((resolve, reject) => {
        Network.get('/top/artists?offset=0&limit=5')
            .then((result) => {
                resolve(result.artists)
            })
            .catch(err => {
                reject(err)
            })
    })
}
// 获取A-Z的方法
export const getLetterArtists = (letter) => {
    return new Promise((resolve, reject) => {
        let lettersArtists = []
        Network.all([
            Network.get(`/artist/list?limit=5&offset=0&type=1&area=7&initial=${letter}`),
            Network.get(`/artist/list?limit=5&offset=0&type=2&area=7&initial=${letter}`),
            Network.get(`/artist/list?limit=5&offset=0&type=3&area=7&initial=${letter}`),
            Network.get(`/artist/list?limit=5&offset=0&type=1&area=96&initial=${letter}`),
            Network.get(`/artist/list?limit=5&offset=0&type=2&area=96&initial=${letter}`),
            Network.get(`/artist/list?limit=5&offset=0&type=3&area=96&initial=${letter}`),
        ])
            .then((result) => {
                // console.log(result);
                result.forEach((item) => {
                    lettersArtists.push(...item.artists)
                })
                // console.log(lettersArtists);
                resolve(lettersArtists)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
// 获取A-Z歌手
export const getAllArtists = (letter) => {
    return new Promise((resolve, reject) => {
        // 收集 热 A-Z
        let keys = ['热']
        // 将获取热门歌手数据的方法和获取A-Z歌手的方法放到一个数组中，给all方法
        let list = [getHotArtists()]
        for (let i = 65; i < 91; i++) {
            // 将数字65到91转成A-Z
            let char = String.fromCharCode(i)
            keys.push(char)
            list.push(getLetterArtists(char))
        }
        // 渲染数据的时候需要用到keys的值和list 所以将数据处理后在返回
        Network.all(list)
            .then((result) => {
                let obj = {}
                obj.keys = keys
                obj.list = result
                // console.log(result);
                resolve(obj)
            })
            .catch(err => {
                reject(err)
            })
    })
}
// 所有榜单内容摘要
export const getToplistDetail = () => {
    return new Promise((resolve, reject) => {
        let category = {
            officialList: [
                { name: '飙升榜', id: 3 },
                { name: '新歌榜', id: 0 },
                { name: '热歌榜', id: 2 },
                { name: '原创榜', id: 1 }
            ],
            recList: [
                { name: '云音乐说唱榜', id: 23 },
                { name: '云音乐电音榜', id: 25 },
                { name: '云音乐欧美新歌榜', id: 32 },
                { name: '网络热歌榜', id: 26 },
                { name: '云音乐电音榜', id: 22 },
                { name: '云音乐摇滚榜', id: 24 }
            ],
            globalList: [
                { name: '美国Billboard榜', id: 6 },
                { name: 'UK排行榜周榜', id: 5 },
                { name: 'Beatport全球电子舞曲榜', id: 21 },
                { name: '日本Oricon榜', id: 10 },
                { name: '越南语榜', id: 8 },
                { name: '俄语榜', id: 29 }
            ],
            otherList: [
                { name: 'KTV唛榜', id: 7 },
                { name: '法国 NRJ Vos Hits 周榜', id: 19 },
                { name: '潜力爆款榜', id: 27 },
                { name: '云音乐韩语榜', id: 28 },
                { name: '听歌识曲榜', id: 30 },
                { name: '云音乐欧美热歌榜', id: 31 }
            ],
            titles: { officialList: '官方榜', recList: '推荐榜', globalList: '全球榜', otherList: '更多榜单' }
        }
        Network.get('/toplist/detail')
            .then((result) => {
                // 接口数据地址http://127.0.0.1:3000/toplist/detail
                // 遍历返回的数据取出里面的name与category中的name做比较
                // 如果相同则把这条数据加进去
                result.list.forEach(item => {
                    // 定义一个布尔值用来当找到对应的数据后跳出循环
                    let flag = false
                    for (let key in category) {
                        // console.log(key);
                        for (let i = 0; i < category[key].length; i++) {
                            if (category[key][i].name === item.name) {
                                category[key][i].rank = item
                                flag = true
                                break
                            }
                        }
                        if (flag) {
                            break
                        }
                    }
                })
                resolve(category)
            })
    })
};
// 获取排行榜
export const getTopList = () => Network.get('/toplist/detail');
// 搜索
export const getSearchList = (data) => Network.get('/search?type=1', data);
// 热门搜索关键字
export const getSearchHot = (data) => Network.get('/search/hot');